#!/bin/bash

set -o pipefail;

PGNAME=`basename $0`
PGDIRNAME=`dirname $0`
PGERR="$PGNAME[$$]: "


if [ "${MLPROD_HOME}" == "" ]
then
  echo "${PGERR} :  MLPROD_HOME env not set;  exiting !!!" >&2
  exit 10
fi

PGCFG_FILE="${MLPROD_HOME}/config/mlprog.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"

CUST_CTX="$(mlget_cust_ctx 'cust')"
MOD_CTX="$(mlget_cust_ctx  'mod')"
BATCH_WFL_ID=1                  # workflow/product id
BATCH_MODE="oper"
if [ ! -z ${BATCH_RUN_MODE} ]
then
  BATCH_MODE="${BATCH_RUN_MODE}"
fi
LOG_CTX="${BATCH_MODE}-${CUST_CTX}"


PGCFG_FILE="${MLPROD_HOME}/cust/${CUST_CTX}/config/${MOD_CTX}.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file sourcing <${PGCFG_FILE}> error; exiting !!!" >&2
  exit 10
fi


function cleanup
{
  rm -f ${EDER_DSTATUS_FILE};
  rm -f ${EDER_ESTATUS_FILE};

  if [ -f ${OUT_INSURANCE_FILE} ]
  then
    rm -f ${OUT_INSURANCE_FILE}
    if [ $? -ne 0 ]
    then
      mllogerr "[${LOG_CTX}] : ERR : Unable to cleanup files : ${OUT_INSURANCE_FILE}";
    fi
  fi

}

trap cleanup EXIT ERR TERM INT QUIT

function usage()
{
  mllogerr "Usage: $PGNAME batch_recv_date_in_MMDDYY_format";
}

if [ $# -lt 1 ]
then
  usage;
  exit 10;
else
  if [ "${2}" == "-s" ]
  then
    skip=1
  else
    skip=0
  fi
fi


# get the batch dates BATCH_RECV_DATE, BATCH_RUN_DATE, BATCH_EXEC_DATE
mlget_batch_dates "${1}"
if [ $? -ne 0 ] || [ -z "${BATCH_RECV_DATE}" ]
then
  usage
  exit 10
fi

# LOG
LOG_CTX="${LOG_CTX}-${BATCH_RECV_DATE}"


# set file locations
if [ ! -d ${BATCH_CTX_DIR} ]
then
  mkdir ${BATCH_CTX_DIR}
  if [ $? -ne 0 ]
  then
    mllogerr "[${LOG_CTX}] : ERR : unable to create batch dir : ${BATCH_CTX_DIR}"
    exit 10;
  fi
fi

# rcpt for encr
#ERCPT_EMDEON="-r mkey_emdeon"
# encr with efx key
ERCPT_EFX="-r mkey_efx" 
# with mdlx key
ERCPT_MDLX="-r mkey_mdlx"
#GPG STATUS FILE
EDER_DSTATUS_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_ins.d
EDER_ESTATUS_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_ins.e
ENCR="${ENCR} -s ${EDER_ESTATUS_FILE}"
DECR="${DECR} -s ${EDER_DSTATUS_FILE}"

# ACCOUNT FILE
IN_INSURANCE_FILE=${ARCH_CTX_IN_DIR}/${BATCH_RECV_DATE}_insacct.gpg
OUT_INSURANCE_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_${BATCH_WFL_ID}_insacct.gpg



if [ ! -r ${IN_INSURANCE_FILE} ]
then
  mllogerr "[${LOG_CTX}] : ERR : Incoming insurance file not found : ${IN_INSURANCE_FILE}";
  exit 10;
fi

if [ -f ${OUT_INSURANCE_FILE} ]
then
#  mllogerr "[${LOG_CTX}] : ERR : Converted insurance file exist in batch area : ${OUT_INSURANCE_FILE}";
#  exit 10;
  rm -f ${OUT_INSURANCE_FILE}
fi


TASK_ID=0
tstart=$((`date +%s`))

tstamp="00:00:00"
mllogerr "[${LOG_CTX}] : BEG : [${tstamp}] : PROCESS PART-1 START"


TASK_ID=`expr $TASK_ID + 1`
tmsg="load sql modules";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${SQL_CTX_CMN_RUNT_DIR}/load_funcs.sh
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))

ts=$((`date +%s`))
${SQL_CTX_RUNT_DIR}/load_funcs.sh
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="custom adjustment to client_npi1 and client_npi2 table in temp_batch insurance0 table ";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/upd_client_npi.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="dedup and load data into batch insurance0 table ";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/load_table_batch_insurance0.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="update batch insurance0 table ";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/upd_table_batch_insurance0.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 1$TASK_ID;
fi
te=$((`date +%s`))


#get efx_append field from insurance0_t table
date=${BATCH_RECV_DATE:0:6}
year=`expr ${date:4} + 2000`
month=${date:0:4}
newdate="$month$year"

TASK_ID=`expr $TASK_ID + 1`
tmsg="insert into insurance0 efx table ";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/load_table_batch_efx.sql
if [ $? -ne 0 ]
then
 	mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
    cleanup;
	exit 1$TASK_ID;
fi
te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="copy batch efx out table";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
NUMLINE=`${JPGSQL} ${SQL_CTX_BAK_DIR}/copy_table_batch_efx.sql | wc -l`;
if [ $NUMLINE -gt 0 ]
then
	# BATCH EFX OUT FILE
	EFX_OUT_FILE=${ARCH_CTX_OUT_DIR}/medlytix${BATCH_RECV_DATE:7:6}${BATCH_RECV_DATE:14:6}_${NUMLINE}_$newdate.na.pgp;
	BACKUP_EFX_OUT_FILE=${ARCH_CTX_OUT_DIR}/${BATCH_RECV_DATE}_efx_bak.gpg;
	${JPGSQL} ${SQL_CTX_BAK_DIR}/copy_table_batch_efx.sql \
	| ${ENCR} ${ERCPT_EFX} -f - > ${EFX_OUT_FILE}
	${JPGSQL} ${SQL_CTX_BAK_DIR}/copy_table_batch_efx.sql \
	| ${ENCR} ${ERCPT_MDLX} -f - >  ${BACKUP_EFX_OUT_FILE}
	te=$((`date +%s`))
fi
if [ $? -ne 0 ]
then
	mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
	cleanup;
	exit 1$TASK_ID;
fi
te=$((`date +%s`))

tstamp="$(mltstamp ${tstart})"

mllogerr "[${LOG_CTX}] : END : [${tstamp}] : PROCESS PART-1 DONE"

exit 0;

